﻿using Furion;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using UpperComputer.Base.EntityFramework.Core;
using UpperComputer.Core;
using UpperComputer.EntityFramework.Core;
using YFurion;
using YFurion.Controllers;
using YFurion.Security;
using YFurion.Security.XSS.Filters;

namespace UpperComputer.Web.Core
{
    [DependsOn(
        typeof(YFurionServiceComponent),
        typeof(UpperComputerBaseEntityFrameworkCoreServiceComponent),
        typeof(UpperComputerEntityFrameworkCoreServiceComponent)
    )]
    public class UpperComputerWebCoreServiceComponent : IServiceComponent
    {
        void IServiceComponent.Load(IServiceCollection services, ComponentContext componentContext)
        {
            IConfiguration configuration = App.Configuration;
            //注册Redis
            IConfigurationSection configurationSection = configuration.GetSection("Redis");
            if (configurationSection.GetValue<bool>("Enabled"))
            {
                services.AddStackExchangeRedisCache(options =>
                {
                    string cache = Des3.Decrypt(configurationSection.GetValue<string>("Cache"), UpperComputerCoreConst.HashPrefix);
                    options.Configuration = cache;
                    options.InstanceName = configurationSection.GetValue<string>("InstanceName");
                });
            }

            //注册App授权
            services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
            //注册跨域
            services.AddCorsAccessor();
            //注册远程请求
            services.AddRemoteRequest();
            //注册脱敏词汇检测服务
            services.AddSensitiveDetection();
            //注册控制器
            services.AddControllers()
                .AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss zz";
                })
                .AddAppLocalization()
                .AddInjectWithUnifyResult();// 注册Furion和规范返回格式

            services.AddMvcFilter<YUnitOfWorkFilter>();
            services.AddMvcFilter<XSSFilter>();
        }
    }
}
